package com.woniuxy.provider;

import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import com.woniuxy.pojo.House;


public class HouseProvider {

	public String select(Map<String, Object> map) {
		House house = (House) map.get("house");
		int pageNumber = (int) map.get("pageNumber");
		int pageSize = (int) map.get("pageSize");
		String sql = new SQL() {
			{
				SELECT("*");
				FROM("house");
				if (house.getAddress() != null) {
                    WHERE("address like " + "'%" + house.getAddress() + "%'");
				}
				if (house.getState() != 0) {
					WHERE("state = #{state}");
				}
				if (house.getHouse_sort() != 0) {
					WHERE("house_sort = #{house_sort}");
				}
				// 从这个toString可以看出，其内部使用高效的StringBuilder实现SQL拼接
			}
		}.toString();
		if (pageSize != 0) {
			sql += " LIMIT " + pageNumber + "," + pageSize;
		}

		return sql;

	}

	public String selectCount(House house) {
		String sql = new SQL() {
			{
				SELECT("count(*)");
				FROM("house");
				if (house.getAddress() != null) {
                    WHERE("address like " + "'%" + house.getAddress() + "%'");
				}
				if (house.getState() != 0) {
					WHERE("state = #{state}");
				}
				if (house.getHouse_sort() != 0) {
					WHERE("house_sort = #{house_sort}");
				}
				// 从这个toString可以看出，其内部使用高效的StringBuilder实现SQL拼接
			}
		}.toString();

		return sql;

	}
}
